﻿using Shop.Model.DB;
using Shop.RemoteModel.GoodsSpec.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Shop.Repository.lmpl
{
    internal class GoodsSkuStateRepository : ShopBaseRepository<DBGoodsSkuState, long>, IGoodsSkuStateRepository
    {
        public GoodsSkuStateRepository (ISqlClientFactory factory) : base(factory)
        {
        }
        public Dictionary<string, bool> GetsSkuState (long goodsId)
        {
            return base.Gets(a => a.GoodsId == goodsId, a => new
            {
                a.SpecsKey,
                a.IsEnable
            }).ToDictionary(a => a.SpecsKey, a => a.IsEnable);
        }

        public void Add (SkuStateSet add)
        {
            base.Insert(new DBGoodsSkuState
            {
                GoodsId = add.GoodsId,
                SpecsKey = add.SpecKey,
                Id = IdentityHelper.CreateId(),
                IsEnable = add.IsEnable
            });
        }
        public void SetIsEnable (long id, bool isEnable)
        {
            if (!base.Update(a => a.IsEnable == isEnable, a => a.Id == id))
            {
                throw new ErrorException("shop.goods.sku.state.set.fail");
            }
        }
        public long[] GetNoEnable (long[] skuId)
        {
            return base.Join<DBGoodsSku, long>((a, b) => a.SpecsKey == b.SpecsKey && skuId.Contains(b.SkuId) && a.IsEnable == false, (a, b) => a.Id);
        }
        public bool CheckIsNoEnable (long[] skuId)
        {
            return base.JoinGet<DBGoodsSku, long>((a, b) => a.SpecsKey == b.SpecsKey && skuId.Contains(b.SkuId) && a.IsEnable == false, (a, b) => a.Id) != 0;
        }
    }
}
